package com.example.survey23.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.survey23.common.lang.QueryPageParam;
import com.example.survey23.common.lang.Result;
import com.example.survey23.entity.Group;
import com.example.survey23.entity.GroupLog;
import com.example.survey23.entity.User;
import com.example.survey23.service.GroupLogService;
import com.example.survey23.service.GroupService;
import com.example.survey23.service.UserService;
import org.apache.ibatis.annotations.Mapper;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private GroupLogService groupLogService;

    @RequiresAuthentication//必须登录认证才能访问的注解
    @GetMapping("/index")
    public  Object index(){
        User user = userService.getById(1);
        return Result.succ(user);
    }
    /**
     *
     *@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     * GET方式无请求体，所以使用@RequestBody接收数据时，
     * 前端不能使用GET方式提交数据，
     * 而是用POST方式进行提交。在后端的同一个接收方法里，
     * @RequestBody与@RequestParam()可以同时使用，@RequestBody最多只能有一个，
     * 而@RequestParam()可以有多个。
     *
     * @Validated注解用于检查user中填写的规则  如果不满足抛出异常
     * 可在GlobalExceptionHandler中捕获此异常 进行自定义 返回数据信息
     */
    @PostMapping("/save")
    public  Result save(@RequestBody User user){
        return userService.save(user) ? Result.succ(null) : Result.fail(null);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)? Result.succ(null) : Result.fail(null);
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam String id){
        return userService.removeById(id)? Result.succ(null) : Result.fail(null);
    }

    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }

    @PostMapping("/listP")
    public Result listP(@RequestBody  User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(!user.getUsername().isEmpty()){
            lambdaQueryWrapper.like(User::getUsername, user.getUsername());
        }
        return Result.succ(userService.list(lambdaQueryWrapper));
    }



    // 得到所有租户数据
    @PostMapping("/listPageTenant")
    public Result listPageC1(@RequestBody QueryPageParam queryPageParam){

        // 查询分页数据 —— 传入当前页码和每页条数
        Page<User> page = new Page<>();
        HashMap param = queryPageParam.getParam();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(param != null){
            String name = (String) param.get("name");
            lambdaQueryWrapper.eq(User::getPermission, 1);
            if(!"null".equals(name) && !StringUtils.isEmpty(name)) {
                lambdaQueryWrapper.like(User::getUsername, (String) param.get("name"));
            }
        }
        IPage result = userService.page(page, lambdaQueryWrapper);
        return Result.succ(result);
    }

    // 得到指定租户的用户数据
    @PostMapping("/listPageUser")
    public Result listPageC2(@RequestBody QueryPageParam queryPageParam){

        // 查询分页数据 —— 传入当前页码和每页条数
        Page<User> page = new Page<>();
        HashMap param = queryPageParam.getParam();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(param != null){
            String name = (String) param.get("name");
           int tenantId = (int)param.get("tenantId");
            lambdaQueryWrapper.eq(User::getPermission, tenantId);
            if(!StringUtils.isEmpty(name)) {
                lambdaQueryWrapper.like(User::getUsername, (String) param.get("name"));
                lambdaQueryWrapper.eq(User::getTenantId,  param.get("tenantId"));
            }
        }
        IPage result = userService.page(page, lambdaQueryWrapper);
        return Result.succ(result);
    }

    @PostMapping("/listGroupById")
    public Result listGroupById(@RequestBody HashMap map){ // 根据群组 id 得到该群组中的所有用户并返回
        String id = (String) map.get("id");
        Long group_id = Long.parseLong(id);
        List<GroupLog> listGroupLog = groupLogService.getGroupLogByGroupId(group_id);
        List<User> listUser = userService.list();
        List<User> res = new ArrayList<>();
        for(GroupLog groupLog: listGroupLog){
            for(User user : listUser){
                if(user.getId() == groupLog.getMember()){
                    res.add(user);
                }
            }
        }
        return Result.succ(res);
    }

    @PostMapping("/deleteMember")
    public Result deleteMember(@RequestBody HashMap map){
        return Result.succ(null);
    }


}
